/*
 * Copyright 2008-2015 GuanAiHui inc. 
 */
package com.pretty.adminapp.pojo.model.um;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.pretty.adminapp.pojo.base.AutoIdBaseEntity;
import com.pretty.adminapp.utils.Md5Util;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.util.Date;

/**
 * Employee:
 *
 * @author Dennis Ge
 */
@Entity
@Table(name = "um_employee")
@DynamicInsert
@DynamicUpdate
public class Employee extends AutoIdBaseEntity {

    @Column(name = "login_name")
    private String loginName;

    @Column(name = "name")
    private String name;

    @Column(name = "email")
    private String email;
    @JsonIgnore
    @Column(name = "salt")
    private String salt;
    @JsonIgnore
    @Column(name = "password")
    private String password;

    @Column(name = "manager_id")
    private Long managerId;

    @Column(name = "department_id")
    private Long departmentId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "department_id", insertable = false, updatable = false)
    private Department department;

    @Column(name = "last_login_on")
    @Temporal(TemporalType.TIMESTAMP)
    protected Date lastLoginOn;
    @JsonIgnore
    @Column(name = "status")
    @Enumerated(EnumType.ORDINAL)
    private EmployeeStatus status;
    @JsonIgnore
    @Column(name = "remark")
    private String remark;
    @Column(name = "icon")
    private String icon;
    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.salt = RandomStringUtils.randomAlphanumeric(10);
        this.password = Md5Util.getMD5(password + salt).toLowerCase();
    }

    public boolean verifyPassword(String password){
        if(StringUtils.isBlank(password)){
            return false;
        }
        String target = Md5Util.getMD5(password + salt).toLowerCase();
        if(target.equals(this.password)){
            return true;
        }
        return false;
    }

    public Long getManagerId() {
        return managerId;
    }

    public void setManagerId(Long managerId) {
        this.managerId = managerId;
    }

    public Long getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(Long departmentId) {
        this.departmentId = departmentId;
    }

    public Date getLastLoginOn() {
        return lastLoginOn;
    }

    public void setLastLoginOn(Date lastLoginOn) {
        this.lastLoginOn = lastLoginOn;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public EmployeeStatus getStatus() {
        return status;
    }

    public void setStatus(EmployeeStatus status) {
        this.status = status;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    @Override
    public String toString() {
        return "Employee [loginName=" + loginName + ", name=" + name + ", email=" + email + ", managerId=" + managerId
                + ", departmentId=" + departmentId + ", department=" + department + ", lastLoginOn=" + lastLoginOn + ", status="
                + status + ", remark=" + remark + ", id=" + id + ", active=" + active + "]";
    }


    public static enum EmployeeStatus {
        Locked, Active
    }

}
